package com.xinbochuang.machine.admin.service.impl.scrap;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.scrap.Scrap;
import com.xinbochuang.machine.admin.domain.scrap.ScrapUse;
import com.xinbochuang.machine.admin.mapper.scrap.ScrapUseMapper;
import com.xinbochuang.machine.admin.service.scrap.IScrapService;
import com.xinbochuang.machine.admin.service.scrap.IScrapUseService;
import com.xinbochuang.machine.common.enums.SimpleCheckStatus;
import com.xinbochuang.machine.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 黄晓鹏
 * @date 2020-10-25
 */
@Service
public class ScrapUseServiceImpl extends ServiceImpl<ScrapUseMapper, ScrapUse> implements IScrapUseService {

    @Resource
    private ScrapUseMapper scrapUseMapper;

    @Resource
    private IScrapService scrapService;

    /**
     * 公共资源申请分页列表
     *
     * @param scrapUse 查询条件
     * @return 结果
     */
    @Override
    public IPage<ScrapUse> selectScarpUseList(ScrapUse scrapUse) {
        return scrapUseMapper.getList(new Page<>(scrapUse.getPageNum(), scrapUse.getPageSize()), scrapUse);
    }

    /**
     * 新增公共资源申请
     *
     * @param scrapUse 参数
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(ScrapUse scrapUse) {
        scrapUse.setCreateBy(SecurityUtils.getUsername());
        scrapUse.setCreateTime(DateUtil.now());
        save(scrapUse);
        ScrapUse info = getById(scrapUse.getId());
        Scrap scrap = new Scrap();
        scrap.setId(info.getScrapId());
        scrap.setUseStatus("1");
        scrapService.updateById(scrap);
        return true;
    }

    /**
     * 公共资源申请审批
     *
     * @param scrapUse 参数
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean changeStatus(ScrapUse scrapUse) {
        scrapUse.setUpdateBy(SecurityUtils.getUsername());
        scrapUse.setUpdateBy(DateUtil.now());
        updateById(scrapUse);
        ScrapUse info = getById(scrapUse.getId());
        Scrap scrap = new Scrap();
        scrap.setId(info.getScrapId());
        if (info.getStatus().equals(SimpleCheckStatus.NOMAIL.getCode())) {
            //审批通过
            scrap.setUseStatus("2");
        } else if (info.getStatus().equals(SimpleCheckStatus.NOPASS.getCode())) {
            //审批不通过
            scrap.setUseStatus("0");
        }
        scrapService.updateById(scrap);
        return true;
    }
}
